﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.IRepository;

namespace CMS.Repository
{
    /// <summary>
    /// CourseListRepository
    /// Author:Fiona Pan
    /// Notes:Take task from Sissi Zeng
    /// </summary>
    public class CourseListRepository : ICourseListRepository
    {
        /// <summary>
        /// Selects courses by conditon.
        /// </summary>
        /// <param name="condition">The Condition.</param>
        /// <returns>List of CourseListModel.</returns>
        public List<Model.CourseList> SelectCourseByConditon(Model.Condition condition,int pageIndex, int pageSize, out int rowCount)
        {
            using (var cmxContext = new CMSEntities())
            {
                bool _status = false;
                if (condition.CourseStatus >= 0)
                {
                    if (condition.CourseStatus == 1)
                    {
                        _status = true;
                    }
                }

                IQueryable<Model.CourseList> courseList = from c in cmxContext.Courses
                                                          join l in cmxContext.Levels
                                                          on c.LevelId equals l.LevelId
                                                          join t in cmxContext.Teachers
                                                          on c.TeacherId equals t.TeacherId
                                                          where ((condition.TeacherName.Length == 0) || (t.TeacherName == condition.TeacherName))
                                                          && ((condition.LevelName.Length == 0) || (l.LevelName == condition.LevelName))
                                                          && ((condition.CourseName.Length == 0) || (c.CourseName.Contains(condition.CourseName)))
                                                          && ((c.CourseStatus == _status)||(condition.CourseStatus < 0))

                                                          select new Model.CourseList
                                                          {
                                                              CourseId=c.CourseId,
                                                              CourseName=c.CourseName,
                                                              CourseStatus=c.CourseStatus,
                                                              TeacherName=c.Teacher.TeacherName,
                                                              LevelName=c.Level.LevelName,
                                                              StudentNumber=c.CourseDetails.Count()
                                                          };
                rowCount = courseList.Count();
                courseList = courseList.OrderBy(c => c.CourseId).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return courseList.ToList();
            }
        }
    }
}
